package com.yoo.chat.kit.http

import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.yoo.chat.kit.entity.IMParamsEntity
import com.yoo.chat.kit.model.ActionModel
import com.yoo.ons.base.BaseVMViewModel
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.base.Request


object APIRequest: BaseVMViewModel() {

    fun addAction(listener: HttpRequestListener<ActionModel>) {
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postAddAction(map)?.requestObserver(
                    NetworkObserver<ActionModel>().startRequest {
                        showLoading()
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        hideLoading()
                        requestEndListener?.invoke()
                    }
                )
            }
        })
    }

    fun openChatPage(targetId:String,type:Int,callback:(entity:IMParamsEntity)->Unit?){
        val listener = object : HttpRequestListener<IMParamsEntity>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
                params["userCodeFriend"] = targetId
                params["type"] = type
            }

            override fun success(t: IMParamsEntity?) {
            }
        }
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postOpenChatPage(map)?.requestObserver(
                    NetworkObserver<IMParamsEntity>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        if (it != null) {
                            callback.invoke(it)
                        }
                    }.error {
                    }.endRequest {
                        listener.endRequest()
                    }
                )

            }

        })
    }

//    fun getEquityReduce(context: Context, userCode: String, callback: OnRequestCallBack, benefitCode: Int, type: Int, imageCode: String, uid: String? = null) {
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setPost(APIPort.openapi_benefit_consume_url)
//                requestBody.add("benefitCode", benefitCode)
//                requestBody.add("chatUserCode", userCode)
//                requestBody.add("imageCode", imageCode)
//                uid?.let { requestBody.add("msgId", it) }
//
//            }
//        }, object : SDOkHttpResultCallBack<BaseEntity>() {
//            override fun onSuccess(entity: BaseEntity) {
//                callback.onSuccess()
//            }
//
//            override fun onFailure(code: Int, msg: String) {
//                when (code) {
//                    2002 -> {
//                        CustomEventUtils.logEvent(if (type == 3) CustomEventTag.Chat_PP_Buy.name else CustomEventTag.Chat_PV_Buy.name)
//                        IMRegisterPayListener.listener.goEquityAcquirePay(context, type, type) {
//                            CustomEventUtils.logEvent(if (type == 3) CustomEventTag.Chat_PP_Buysuccess.name else CustomEventTag.Chat_PV_Buysuccess.name)
//                            getEquityReduce(context, userCode, callback, benefitCode, type, imageCode)
//                        }
//                    }
//
//                    2003 -> {
//                        CustomEventUtils.logEvent(if (type==3)CustomEventTag.Chat_PP_Sub.name else CustomEventTag.Chat_PV_Sub.name)
//                        IMRegisterPayListener.listener.goVipPay(context, type, 1,userCode) {
//                            CustomEventUtils.logEvent(if (type==3)CustomEventTag.Chat_PP_Subsuccess.name else CustomEventTag.Chat_PV_Subsuccess.name)
//                            BaseConfig.getInstance.setBoolean(SpArgument.isMember, true)
//                        }
//                    }
//                }
//            }
//        })
//    }
//
//    fun vipPopupShow(context: Context, callback: OnSuccessCallBack) {
//        CustomEventUtils.logEvent(CustomEventTag.Chat_PrivateAlbum_Sub.name)
//        IMRegisterPayListener.listener.goVipPay(context, 5, 1,"") {
//            CustomEventUtils.logEvent(CustomEventTag.Chat_PrivateAlbum_Subsuccess.name)
//            BaseConfig.getInstance.setBoolean(SpArgument.isMember, true)
//            callback.onSuccess()
//        }
//    }
//
//    fun getUserPhotoInfo(callback: OnUserPhotoInfoCallBack) {
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setPost(APIPort.openapi_user_web_url)
//            }
//        }, object : SDOkHttpResultCallBack<UserPhotoInfoEntity>() {
//            override fun onSuccess(entity: UserPhotoInfoEntity) {
//                callback.onSuccess(entity)
//            }
//        })
//    }
//
//    fun getUserPhotoInfoEmpty(
//        context: Context,
//        callback: OnSuccessCallBack
//    ) {
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setPost(APIPort.openapi_user_web_url)
//            }
//        }, object : SDOkHttpResultCallBack<UserPhotoInfoEntity>() {
//            override fun onSuccess(entity: UserPhotoInfoEntity) {
//                if (entity.data.isMember) {
//                    callback.onSuccess()
//                } else {
//                    IMRegisterPayListener.listener.goVipPay(context, 5, 1,"") {
//                        BaseConfig.getInstance.setBoolean(SpArgument.isMember, true)
//                    }
//                }
//            }
//        })
//    }
//
//    fun getPrivateAlbum(callBack: OnPersonageAlbumCallBack) {
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setPost(APIPort.openapi_user_albums_list_url)
//                requestBody.add("type", 3)
//            }
//
//        }, object : SDOkHttpResultCallBack<PersonageAlbumEntity>() {
//            override fun onSuccess(entity: PersonageAlbumEntity) {
//                callBack.onSuccess(entity)
//            }
//        })
//    }
//
//    fun canSendMessage(targetId: String, context: Context, callback: OnRequestCallBack) {
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setPost(APIPort.openapi_chat_msg_try_send_url)
//                requestBody.add("toUserId", targetId)
//            }
//
//        }, object : SDOkHttpResultCallBack<BaseEntity>() {
//            override fun onSuccess(entity: BaseEntity) {
//                callback.onSuccess()
//            }
//
//            override fun onFailure(code: Int, msg: String) {
//                super.onFailure(code, msg)
//                when (code) {
//                    3004, 3007, 3008, 3009 -> {
//                        callback.onFailure(code, msg)
//                        IMRegisterPayListener.listener.goVipPay(context, 0, 1,targetId) {
//                            BaseConfig.getInstance.setBoolean(SpArgument.isMember, true)
//                        }
//                    }
//                }
//            }
//        })
//    }
//
//    fun addPhotoToAlbums(path: String, videoLength: Long) {
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setPost(APIPort.openapi_user_albums_add_url)
//                requestBody.add("imageUrl", path)
//                requestBody.add("type", 3)
//                requestBody.add("videoLength", videoLength)
//
//            }
//        }, object : SDOkHttpResultCallBack<BaseEntity>() {
//            override fun onSuccess(entity: BaseEntity) {
//            }
//
//            override fun onFailure(code: Int, msg: String) {
//            }
//        })
//    }
//
//    fun getIMParams(targetId: String, type: Int, callback: RequestTurnOnsSuccessCallBack) {
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setPost(APIPort.openapi_chat_open_url)
//                requestBody.add("userCodeFriend", targetId)
//                requestBody.add("type", type)
//                if (type == 2) {
//                    requestBody.add("sendMessagesNumber", BaseConfig.getInstance.getInt("${targetId}_sendMessagesNumber", 0))
//                }
//            }
//        }, object : SDOkHttpResultCallBack<IMParamsEntity>() {
//            override fun onSuccess(entity: IMParamsEntity) {
//                if (type == 1) {
//                    BaseConfig.getInstance.setInt(SpArgument.conversationList + targetId, entity.data.onlineStatus)
//                    BaseConfig.getInstance.setInt(SpArgument.conversationFlashList + targetId, entity.data.flashchatFlag)
//
//                    BaseConfig.getInstance.setInt("${targetId}_sendMessagesNumber", 0)
//                    BaseConfig.getInstance.setInt("${targetId}_messagesNumber", entity.data.messagesNumber)
//                    BaseConfig.getInstance.setBoolean("${targetId}_member", entity.data.member)
//                    callback.onSuccess(entity.data)
//                } else {
//                    if (entity.data.effectiveChat) {
//                        val userInfo = RongUserInfoManager.getInstance().getUserInfo(targetId)
//                        SDEventManager.post(userInfo.name, EnumEventTag.SHOW_NOTIFICATION_DIALOG.ordinal)
//                    }
//                }
//            }
//            override fun onFailure(code: Int, msg: String) {
//            }
//        })
//    }
//
//    interface OnRequestCallBack {
//        fun onSuccess()
//        fun onFailure(code: Int, msg: String)
//    }
//
//    interface OnSuccessCallBack {
//        fun onSuccess()
//    }
//
//    interface RequestTurnOnsSuccessCallBack {
//        fun onSuccess(entity: IMParamsEntity.Data)
//    }
//
//    interface OnUserPhotoInfoCallBack {
//        fun onSuccess(entity: UserPhotoInfoEntity)
//    }
//
//    interface OnPersonageAlbumCallBack {
//        fun onSuccess(entity: PersonageAlbumEntity)
//    }
//
//
//    fun showVIPPopup(context: Context, type: Int, targetId:String) {
//        IMRegisterPayListener.listener.goVipPay(context, type, 1,targetId) {
//            BaseConfig.getInstance.setBoolean(SpArgument.isMember, true)
//            BaseConfig.getInstance.setBoolean(targetId + "_member", true)
//        }
//    }
//
//    fun getIMFlirting(callback: (result: IMFlirtingEntity) -> Unit){
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setGet(APIPort.openapi_say_hi_flirting_url)
//            }
//        }, object : SDOkHttpResultCallBack<IMFlirtingEntity>() {
//            override fun onSuccess(entity: IMFlirtingEntity) {
//                callback.invoke(entity)
//            }
//
//            override fun onFailure(code: Int, msg: String) {
//            }
//        })
//    }
//
//    fun getIMGreetingMessage(callback: (result: IMGreetingEntity) -> Unit?){
//        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
//            override fun addBody(requestBody: OkHttpBodyEntity) {
//                requestBody.setGet(APIPort.openapi_say_hi_content_url)
//            }
//        }, object : SDOkHttpResultCallBack<IMGreetingEntity>() {
//            override fun onSuccess(entity: IMGreetingEntity) {
//                callback.invoke(entity)
//            }
//
//            override fun onFailure(code: Int, msg: String) {
//            }
//        })
//    }

}